package mvc.mainPage;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import mvc.IModel;
import mvc.IModelCreator;

public class MainModelCreator implements IModelCreator {
	
		private static Logger log = Logger.getLogger(MainModelCreator.class);
	
		@Override
	    public IModel createModel(HttpServletRequest request) {
			
		MainModel mainModel = new MainModel();
		String login = request.getParameter("login");
		String password = request.getParameter("password");
		
		mainModel.setUserLogin(login);
		mainModel.setUserPassword(password);
		
		
		String currentRoomId = request.getParameter("currentRoomId");
		if (currentRoomId == null || currentRoomId.isEmpty()) {		
			currentRoomId = request.getSession().getAttribute("currentRoomId").toString();			
		} else {
			request.getSession().setAttribute("currentRoomId", currentRoomId);
		}	
		
		log.debug("currentRoomId = " + currentRoomId);
		mainModel.setCurrentRoomId( (currentRoomId != null ? Integer.valueOf(currentRoomId) : null) );
		
		String currentUserId = null;
		if (request.getSession().getAttribute("currentUserId") != null) {
			currentUserId = request.getSession().getAttribute("currentUserId").toString();
		}
				
		mainModel.setCurrentUserId( currentUserId != null ? Integer.valueOf(currentUserId) : null );
		log.debug("currentUserId = " + currentUserId);
		
		return mainModel;
	}

}
